package main

import (
	"encoding/gob"
	"fmt"
	"log"
	"os"
)

type Address struct {
	Type    string
	City    string
	Country string
}

type VCard struct {
	FirstName string
	LastName  string
	Addresses []*Address
	Remark    string
}

var content string

func read(f string) {
	file1, _ := os.Open(f)
	decoder := gob.NewDecoder(file1)
	var dv VCard
	err2 := decoder.Decode(&dv)
	if err2 != nil {
		log.Println("Error in encoding gob")
	}
	fmt.Printf(" %v  ", dv)
	for _, v := range dv.Addresses {
		fmt.Printf(" %v ", v)
	}
}

func write(f string) {
	pa := &Address{"private", "Aartselaar", "Belgium"}
	wa := &Address{"work", "Boom", "Belgium"}
	vc := VCard{"Jan", "Kersschot", []*Address{pa, wa}, "none"}
	file, _ := os.OpenFile(f, os.O_CREATE|os.O_WRONLY, 0666)
	defer file.Close()
	encoder := gob.NewEncoder(file)
	err := encoder.Encode(vc)
	if err != nil {
		log.Println("Error in encoding gob")
	}
}

func main() {
	s := "vcard1.gob"
	write(s)
	read(s)
}
